<template>
  <div class="book-detail-info">
    <div class="cover">
      <image
        class="bg-img"
        :src="detail.image"
        mode="scaleToFill"
        :lazy-load="true"
      />
      <div class="top-img-box">
        <image
          class="top-img"
          :src="detail.image"
          mode="aspectFit"
          :lazy-load="true"
          @click="previewImg"
        />
        <div class="title primary-color">{{ detail.title }}</div>
        <div class="author">{{ author }}</div>
      </div>
    </div>
    <div class="detail">
      <div class="top">
        <div class="top-left">
          <image
            class="avatar"
            :src="userInfo.avatarUrl"
            mode="aspectFit"
            :lazy-load="true"
          />
          <span class="nick-name">{{ userInfo.nickName }}</span>
        </div>
         <div class="top-right">
          <span class="rate-text primary-color">{{ detail.avrgRating }}</span>
          <Rate :value="detail.avrgRating" />
        </div>
      </div>
      <div class="bottom">
        <span>{{ detail.publisher }}</span>
        <span>{{ detail.price }}</span>
      </div>
      <div class="tags">
        <span
          class="tag-item"
          v-for="(tag, index) in detail.formatTags"
          :key="index"
        >
          {{ tag }}
        </span>
      </div>
      <div class="summary">
       <p v-for="(paragraph, index) in summary" :key="index">
         {{ paragraph }}
       </p>
      </div>
    </div>
  </div>
</template>

<script>
  import Rate from '@/components/Rate';
  export default {
    components: {
      Rate,
    },
    props: {
      detail: {
        type: Object,
        default: {},
      },
    },
    methods: {
      previewImg () {
        wx.previewImage({
          urls: [this.detail.image] // 需要预览的图片http链接列表
        })
      },
    },
    computed: {
      author () {
        const { author = [] } = this.detail;
        return author.join(); // 默认是以‘，’分隔 
      },
      userInfo() {
        const { userId = {} } = this.detail;
        return userId;
      },
      summary () {
        // 方法一
        const { summary = '' } = this.detail;
        return summary.split('\n');
        // 方法二，将\n替换为p标签，summary首尾分别加上<p>和</p>,相对麻烦
      }
    },
  }
</script>

<style lang="less" scoped>
@primary-text-color: #2d8cf0;
.book-detail-info {
  .cover {
    position: relative;
    overflow: hidden;
    width: 100%;
    height: 400rpx;
    .bg-img {
      width: 100%;
      height: 100%;
      filter: blur(30rpx); // 模糊背景图
    }
    .top-img-box {
      width: 100%;
      height: 300rpx;
      position: absolute;
      top: 10rpx;
      left: 0;
      text-align: center;
      .top-img {
        height: 100%;
      }
      .title {
        font-size: 12px;
      }
      .author {
        color: #bbb;
        font-size: 11px;
      }
    }
  }
  .detail {
    margin: 0 20rpx;
    .top,
    .bottom {
      display: flex;
      justify-content: space-between;
      font-size: 12px;
      margin-top: 10rpx;
      .top-left,
      .top-right {
        display: flex;
        align-items: center;
        height: 64rpx;
        .avatar {
          height: 100%;
          width: 64rpx;
          border-radius: 50%;
        }
        .nick-name {
          margin-left: 10rpx;
        }
        .rate-text {
          margin-right: 10rpx;
        }
      }
    }
    .tags {
      font-size: 12px;
      display: flex;
      flex-wrap: wrap;
      .tag-item {
        margin: 10rpx 10rpx 0 0;
        padding: 0 5rpx;
        border-radius: 10rpx;
        color: @primary-text-color;
        border: 1px solid @primary-text-color;
      }
    }
    .summary {
      margin: 10rpx 0;
      font-size: 12px;
      color: gray;
      p {
        text-align: justify;
        text-indent: 2em;
      }
    }
  }
}
</style>